package com.pactera.madp.cp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pactera.madp.common.core.exception.CheckedException;
import com.pactera.madp.cp.api.dto.channel.ChannelCodeGroupListVO;
import com.pactera.madp.cp.api.dto.channel.ReqChannelCodeGroupAddDTO;
import com.pactera.madp.cp.api.entity.channel.ChannelCodeEntity;
import com.pactera.madp.cp.api.entity.channel.ChannelCodeGroupEntity;
import com.pactera.madp.cp.api.dto.channel.ReqChannelCodeGroupUpdateDTO;
import com.pactera.madp.cp.handler.CorpContextHolder;
import com.pactera.madp.cp.mapper.channel.ChannelCodeGroupMapper;
import com.pactera.madp.cp.service.IChannelCodeService;
import com.pactera.madp.cp.service.IChannelCodeGroupService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author: yangpengwei
 * @time: 2021/2/22 5:06 下午
 * @description 渠道码分组服务实现类
 */
@Slf4j
@Service
@Transactional(rollbackFor = {RuntimeException.class, Exception.class})
public class ChannelCodeServiceGroupImpl extends ServiceImpl<ChannelCodeGroupMapper, ChannelCodeGroupEntity> implements IChannelCodeGroupService {

    @Autowired
    private IChannelCodeService channelCodeService;

    @Override
    public void saveByNames(ReqChannelCodeGroupAddDTO dto) {
        QueryWrapper<ChannelCodeGroupEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("name", dto.getNameList());
        queryWrapper.eq("corp_id",CorpContextHolder.getCorpId());
        int count = count(queryWrapper);
        if (count > 0) {
            throw new CheckedException("分组名已存在");
        }

        List<ChannelCodeGroupEntity> codeGroupEntityList = new ArrayList<>();
        for (String name : dto.getNameList()) {
            ChannelCodeGroupEntity entity = new ChannelCodeGroupEntity();
            entity.setName(name);
            entity.setCorpId( CorpContextHolder.getCorpId());
            codeGroupEntityList.add(entity);
        }
        saveBatch(codeGroupEntityList);
    }

    @Override
    public void updateNameByGroupId(ReqChannelCodeGroupUpdateDTO dto) {
        QueryWrapper<ChannelCodeGroupEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",dto.getName());
        queryWrapper.eq("corp_id",CorpContextHolder.getCorpId());
        int count = count(queryWrapper);
        if (count > 0) {
            throw new CheckedException("分组名已存在");
        }

        ChannelCodeGroupEntity entity = getById(dto.getGroupId());
        if (Objects.isNull(entity)) {
            throw new CheckedException("修改失败，分组不存在");
        }

        entity = new ChannelCodeGroupEntity();
        entity.setId(dto.getGroupId());
        entity.setName(dto.getName());
        entity.setCorpId( CorpContextHolder.getCorpId());
        entity.updateById();
    }

    @Override
    public List<ChannelCodeGroupEntity> getListByCorpId(Integer corpId) {
        QueryWrapper<ChannelCodeGroupEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("corp_id", corpId);
        return list(queryWrapper);
    }

    @Override
    public boolean deleteCodeGroup(Integer id) {
        List<ChannelCodeEntity> list = channelCodeService.list(Wrappers.<ChannelCodeEntity>lambdaQuery()
                .eq(ChannelCodeEntity::getGroupId, id)
                .eq(ChannelCodeEntity::getCorpId,  CorpContextHolder.getCorpId()));
        if (list.isEmpty()){
            return removeById(id);
        }else {
            throw new CheckedException("当前分组下仍有渠道活码数据，无法删除");
        }
    }

    @Override
    public List<ChannelCodeGroupListVO> codeGroupList() {
        List<ChannelCodeGroupEntity> codeGroupEntityList = list(Wrappers.<ChannelCodeGroupEntity>lambdaQuery().eq(ChannelCodeGroupEntity::getCorpId, CorpContextHolder.getCorpId()));
        List<ChannelCodeGroupListVO> list = new ArrayList<>();
        for (ChannelCodeGroupEntity e : codeGroupEntityList) {
            ChannelCodeGroupListVO vo = new ChannelCodeGroupListVO();
            BeanUtil.copyProperties(e,vo);
            int count = channelCodeService.count(Wrappers.<ChannelCodeEntity>lambdaQuery()
                    .eq(ChannelCodeEntity::getGroupId, e.getId())
                    .eq(ChannelCodeEntity::getCorpId,  CorpContextHolder.getCorpId()));
            vo.setCount(count);
            list.add(vo);
        }
        ChannelCodeGroupListVO vo = new ChannelCodeGroupListVO();
        vo.setId(0);
        vo.setName("全部");
        vo.setCount(channelCodeService.count(Wrappers.<ChannelCodeEntity>lambdaQuery()
                .eq(ChannelCodeEntity::getCorpId,  CorpContextHolder.getCorpId())));
        list.add(vo);
        list.sort(Comparator.comparing(ChannelCodeGroupListVO::getId));
        return list;
    }
}
